package Future;

import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ThreadTest {
	public static double generate(int max) {// 质数过滤算法
		long start = System.currentTimeMillis();
		boolean sieve[] = new boolean[max];
		Arrays.fill(sieve, true);
		sieve[0] = false;
		sieve[1] = false;
		for (int i = 2; i < Math.sqrt(max); i++) {
			if (sieve[i]) {
				for (int j = 2 * i; j < sieve.length; j += i) {
					sieve[j] = false;
				}
			}
		}
		return (System.currentTimeMillis() - start);
	}
	public static void main(String[] args) throws InterruptedException {
		
		
		final ExecutorService exec = Executors.newFixedThreadPool(1);
		Callable call = new Callable() {
			public Object call() throws Exception {
				System.out.println("开始睡觉");
				Thread.sleep(1000);
				System.out.println("睡完了");
				return "线程执行完成";
			}
		};
		Future<String> future = exec.submit(call);
		try {
			exec.shutdown();//关闭线程池
			String obj = future.get(100 * 1, TimeUnit.MILLISECONDS);
			System.out.println("处理" + obj);
		} catch (TimeoutException e) {
			System.out.println("处理超时");
			exec.shutdownNow();
		} catch (Exception e) {//关闭线程
			System.out.println("处理失败");
		} 
		System.out.println("线程完毕");
		
	}
}
